package org.lmind.srd35.combat

trait CombatEnvironment {

  var _objects = List[CombatUnit]()
  
  def objects = _objects
  
  def getObjects(position:(Int, Int)):List[EnvironmentUnit]
  
  def changePosition(o:CombatUnit, position:(Int, Int))
  
  def canMove(units:List[EnvironmentUnit]):Boolean = {
    for (val u <- units) {
      if (!u.canOtherPlace) {
        return false
      }
    }
    true
  }
  
  def moveRange(p:(Int, Int), d:Int):List[(Int, Int)] = {
    
    var canMoveList = List[(Int, Int)]()
    
    def cMove(p:(Int, Int)) = {
      canMove(getObjects(p))
    }
    
    def f(p:(Int, Int), d:Int):Unit = {
      
      if (d == 0)
        return
      if (!canMoveList.contains(p)) {
        canMoveList = p :: canMoveList
      }
      
      if (cMove(p._1, p._2 - 1)) {
        // 上
        f((p._1 + 1, p._2), d - 1)
      }
      if (cMove(p._1 + 1, p._2)) {
        // 右
        f((p._1, p._2 - 1), d - 1)
      }
      if (cMove(p._1, p._2 + 1)) {
        // 下
        f((p._1, p._2 + 1), d - 1)
      }
      if (cMove(p._1 - 1, p._2)) {
        // 左
        f((p._1 - 1, p._2), d - 1)
      }
    }
    f(p, d)
    canMoveList
  }
}